%     FASTENER DOCTOR - программа для лечения и конвертации крепежа
%     Copyright (C) 2018  Михаил Козлов
%     Сайт проекта: <http://www.fastenerdoctor.ru/>
%     Адрес электронной почты для связи с разработчиком:
%     <doctor.fastener@gmail.com>
% 
%     Эта программа является свободным программным обеспечением: Вы можете
%     распространять ее и/или изменять, соблюдая условия Универсальной общественной
%     лицензии GNU, опубликованной Фондом свободного программного обеспечения,
%     либо редакции 3, либо (на Ваше усмотрение) любой редакции, выпущенной позже.
% 
%     Эта программа распространяется в надежде на то, что она окажется полезной,
%     но БЕЗ КАКИХ-ЛИБО ГАРАНТИЙ, включая даже подразумеваемую гарантию ТОВАРНОЙ ПРИГОДНОСТИ,
%     либо ПРИГОДНОСТИ ДЛЯ ОПРЕДЕЛЕННЫХ ЦЕЛЕЙ. Ознакомьтесь с Универсальной общественной
%     лицензией GNU для получения более подробной информации.
% 
%     Вы должны были получить копию Универсальной общественной лицензии GNU
%     вместе с этой программой. Если Вы ее не получили, то перейдите по адресу:
%     <http://www.gnu.org/licenses/>.
% 
%     This program is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     (at your option) any later version.
% 
%     This program is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
%     GNU General Public License for more details.
% 
%     You should have received a copy of the GNU General Public License
%     along with this program.  If not, see <https://www.gnu.org/licenses/>.

function MATERIALS = Read_MATERIALS(Fin, N_Materials, Material_start_position, Mat_Rec_start_position)

% Внимание!
% Заполнение поля .Patran_name не гарантируется, так как случай, когда один комментарий с именем идет на несколько карточек,
% во ряде случаев не отличим от присутствия карточек без комментария. Пример: если 3d ортотропный материал используются
% и в ламинате, и в твердотельных элементах, то для этого материала пишутся две карточки: MAT8 и MAT9 под разными номерами,
% но с одним комментарием. Без сравнения свойств нельзя понять, один и тот же это материал, или нет. Пользуйтесь полем
% .Patran_name лишь как информативным.

% MATERIALS (карточка MAT1) | .card | .ID | .Patran_name | .E | .G | .NU | .RHO | .A | .TREF | .GE | .ST | .SC | .SS | .MCSID |
% MATERIALS (карточка MAT2) | .card | .ID | .Patran_name | .G11 | .G12 | .G13 |
%                                                               | .G22 | .G23 |
%                                                                      | .G33 |
%                                                                      | .RHO | .A1 | .A2 | .A3 | .TREF | .GE | .ST | .SC | .SS | .MCSID | .GE11 | .GE12 | .GE13 | .GE22 | .GE23 | .GE33 |
% MATERIALS (карточка MAT8) | .card | .ID | .Patran_name | .E1 | .E2 | .NU12 | .G12 | .G1Z | .G2Z | .RHO | .A1 | .A2 | .TREF | .Xt | .Xc | .Yt | .Yc | .S | .GE | .F12 | .STRN |
% MATERIALS (карточка MAT9) | .card | .ID | .Patran_name | .G11 | .G12 | .G13 | .G14 | .G15 | .G16 |
%                                                               | .G22 | .G23 | .G24 | .G25 | .G26 |
%                                                                      | .G33 | .G34 | .G35 | .G36 |
%                                                                             | .G44 | .G45 | .G46 |
%                                                                                    | .G55 | .G56 |
%                                                                                           | .G66 |
%                                                                                           | .RHO | .A1 | .A2 | .A3 | .A4 | .A5 | .A6 | .TREF | .GE | .GE11 | .GE12 | .GE13 | .GE14 | .GE15 | .GE16 | .GE22 | .GE23 | .GE24 | .GE25 | .GE26 | .GE33 | .GE34 | .GE35 | .GE36 | .GE44 | .GE45 | .GE46 | .GE55 | .GE56 | .GE66 |
%                                                                                           Расшифровка номеров сдвиговых компонент: 4 - 12, 5 - 23, 6 - 31
%
%    nu12*E2 = nu21*E1
%    nu23*E3 = nu32*E2
%    nu13*E3 = nu31*E1

global ErrCode;
global Progress_Bar_Segments;
global Progress_Bar_Delay;

fprintf (1, '    Чтение материалов...                ');

% Настройка размера массива предыдущих строк
N_previous_strings = 5; % Число должно быть >= 1

MATERIALS = cell(N_Materials,1);

if (Mat_Rec_start_position < Material_start_position)
    fseek(Fin, Mat_Rec_start_position, 'bof');
else
    fseek(Fin, Material_start_position, 'bof');
end

Material_read_counter = 0;


% Для вывода прогресса
Time_segments = Progress_Bar_Segments;
N_records = N_Materials;
N_rec_segm = floor(N_records/Time_segments);
Counter = 0;
Counter_2 = 0;
Written_count = 0;


string = ' ';
Previous_strings = cell(N_previous_strings,1);
for i=1:N_previous_strings
    Previous_strings{i} = ' ';
end


while (~feof(Fin))
    
    if (Material_read_counter == N_Materials)
        break;        
    end
    
    % Чтение следующей строки
    N_prev_str = size(Previous_strings,1);
    for p=1:N_prev_str-1
        Previous_strings{N_prev_str+1-p} = Previous_strings{N_prev_str-p};
    end
    Previous_strings{1} = string;
    string = fgets(Fin);
    
    if (string(1) == 'M')
        
        % Чтение MAT8

        if ((length(string) >= 5) && strcmp(string(2:5), 'AT8 ')) % SMALL FIELD FORMAT

            Material_read_counter = Material_read_counter + 1;

            MATERIALS{Material_read_counter}.card = 'MAT8';
            MATERIALS{Material_read_counter}.ID = [];
            MATERIALS{Material_read_counter}.Patran_name = [];
            MATERIALS{Material_read_counter}.E1 = [];
            MATERIALS{Material_read_counter}.E2 = [];
            MATERIALS{Material_read_counter}.NU12 = [];
            MATERIALS{Material_read_counter}.G12 = [];
            MATERIALS{Material_read_counter}.G1Z = [];
            MATERIALS{Material_read_counter}.G2Z = [];
            MATERIALS{Material_read_counter}.RHO = [];
            MATERIALS{Material_read_counter}.A1 = [];
            MATERIALS{Material_read_counter}.A2 = [];
            MATERIALS{Material_read_counter}.TREF = [];
            MATERIALS{Material_read_counter}.Xt = [];
            MATERIALS{Material_read_counter}.Xc = [];
            MATERIALS{Material_read_counter}.Yt = [];
            MATERIALS{Material_read_counter}.Yc = [];
            MATERIALS{Material_read_counter}.S = [];
            MATERIALS{Material_read_counter}.GE = [];
            MATERIALS{Material_read_counter}.F12 = [];
            MATERIALS{Material_read_counter}.STRN = [];

            % Чтение ID
            if (length(string) < 16)
                End_index = length(string);
            else
                End_index = 16;
            end
            tmp_number = str2double(string(9:End_index));
            if (isnan(tmp_number))
                fprintf (1, '\nНеправильная запись с номером материала в карточке MAT8 : [%s] !\n', strtrim(string(9:End_index)));
                ErrCode = -1;
                return;
            end
            MATERIALS{Material_read_counter}.ID = tmp_number;

            % проверка на записи в 10-ом поле
            if (deblank(string) > 72)
                fprintf (1, '\nОбнаружена запись в поле 10 первой строки карточки MAT8 %i !\n', MATERIALS{Material_read_counter}.ID);
                fprintf (1, 'Указатели продления карточки не поддерживаются в текущей версии программы.\n');
                fprintf (1, 'Обратетись к разработчику для решения этого вопроса.\n');
                ErrCode = -1;
                return;
            end

            % Чтение Patran_name
            if ((length(Previous_strings{1} ) >= 25) && strcmp(Previous_strings{1} (1:25), '$ Description of Material'))
                if ((length(Previous_strings{2} ) >= 20) && strcmp(Previous_strings{2} (1:19), '$ Material Record :')) % здесь нет ошибки с 20 и 19
                    MATERIALS{Material_read_counter}.Patran_name = strtrim(Previous_strings{2}(20:length(Previous_strings{2})));
                else
                    if ((length(Previous_strings{2} ) >= 3) && strcmp(Previous_strings{2}(1:2), '$ ')) % здесь нет ошибки с 3 и 2
                        if ((length(Previous_strings{3} ) >= 20) && strcmp(Previous_strings{3} (1:19), '$ Material Record :')) % здесь нет ошибки с 20 и 19
                            MATERIALS{Material_read_counter}.Patran_name = [strtrim(Previous_strings{3}(20:length(Previous_strings{3}))) strtrim(Previous_strings{2}(3:length(Previous_strings{2})))];
                        end
                    end
                end
            end

            % --- УНИВЕРСАЛЬНЫЙ АЛГОРИТМ ДЛЯ КАРТОЧЕК С ФИКСИРОВАННЫМ ЧИСЛОМ НЕОБЯЗАТЕЛЬНЫХ ПОЛЕЙ ---

            % Установка информационных параметров
            Card_name = 'MAT8';
            Item_ID = MATERIALS{Material_read_counter}.ID;

            % Установка стартовых параметров
            Previous_index = 16;
            Record_length = 8;
            Next_string_large = 0;
            Record_read_counter = 1; % Отсчет прочтенных полей начинается со второго поля карточки
            End_card_flag = 0;

            % Чтение полей карточки
            Results_array = cell(18,1);

            for i=1:size(Results_array,1)

                [Read_Result, Fin, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings] = Read_Field_RB(Fin, Card_name, Item_ID, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings);

                if (ErrCode < 0)
                    break;
                end

                Results_array{i} = Read_Result;

            end

            if (ErrCode < 0)
                return;
            end

            % --- E1 ---                    
            if (~isempty(Results_array{1}))
                MATERIALS{Material_read_counter}.E1 = Results_array{1};
            end

            % --- E2 ---
            if (~isempty(Results_array{2}))
                MATERIALS{Material_read_counter}.E2 = Results_array{2};
            end

            % --- NU12 ---
            if (~isempty(Results_array{3}))
                MATERIALS{Material_read_counter}.NU12 = Results_array{3};
            end

            % --- G12 ---
            if (~isempty(Results_array{4}))
                MATERIALS{Material_read_counter}.G12 = Results_array{4};
            end

            % --- G1Z ---
            if (~isempty(Results_array{5}))
                MATERIALS{Material_read_counter}.G1Z = Results_array{5};
            end

            % --- G2Z ---
            if (~isempty(Results_array{6}))
                MATERIALS{Material_read_counter}.G2Z = Results_array{6};
            end

            % --- RHO ---
            if (~isempty(Results_array{7}))
                MATERIALS{Material_read_counter}.RHO = Results_array{7};
            end

            % --- A1 ---
            if (~isempty(Results_array{8}))
                MATERIALS{Material_read_counter}.A1 = Results_array{8};
            end

            % --- A2 ---
            if (~isempty(Results_array{9}))
                MATERIALS{Material_read_counter}.A2 = Results_array{9};
            end

            % --- TREF ---
            if (~isempty(Results_array{10}))
                MATERIALS{Material_read_counter}.TREF = Results_array{10};
            end

            % --- Xt ---
            if (~isempty(Results_array{11}))
                MATERIALS{Material_read_counter}.Xt = Results_array{11};
            end

            % --- Xc ---
            if (~isempty(Results_array{12}))
                MATERIALS{Material_read_counter}.Xc = Results_array{12};
            end

            % --- Yt ---
            if (~isempty(Results_array{13}))
                MATERIALS{Material_read_counter}.Yt = Results_array{13};
            end

            % --- Yc ---
            if (~isempty(Results_array{14}))
                MATERIALS{Material_read_counter}.Yc = Results_array{14};
            end

            % --- S ---
            if (~isempty(Results_array{15}))
                MATERIALS{Material_read_counter}.S = Results_array{15};
            end

            % --- GE ---
            if (~isempty(Results_array{16}))
                MATERIALS{Material_read_counter}.GE = Results_array{16};
            end

            % --- F12 ---
            if (~isempty(Results_array{17}))
                MATERIALS{Material_read_counter}.F12 = Results_array{17};
            end

            % --- STRN ---
            if (~isempty(Results_array{18}))
                MATERIALS{Material_read_counter}.STRN = Results_array{18};
            end


            % Для вывода прогресса
            Counter = Counter + 1;
            Counter_2 = Counter_2 + 1;
            if (Counter_2 == N_rec_segm)
                Counter_2 = 0;
                Written_count = Written_count + 1;
                if (Written_count < Time_segments)
                    fprintf (1, '#');
                    pause (Progress_Bar_Delay);
                end
            end


            continue;

        end

        if ((length(string) >= 5) && strcmp(string(2:5), 'AT8*')) % LARGE FIELD FORMAT

            Material_read_counter = Material_read_counter + 1;

            MATERIALS{Material_read_counter}.card = 'MAT8';
            MATERIALS{Material_read_counter}.ID = [];
            MATERIALS{Material_read_counter}.Patran_name = [];
            MATERIALS{Material_read_counter}.E1 = [];
            MATERIALS{Material_read_counter}.E2 = [];
            MATERIALS{Material_read_counter}.NU12 = [];
            MATERIALS{Material_read_counter}.G12 = [];
            MATERIALS{Material_read_counter}.G1Z = [];
            MATERIALS{Material_read_counter}.G2Z = [];
            MATERIALS{Material_read_counter}.RHO = [];
            MATERIALS{Material_read_counter}.A1 = [];
            MATERIALS{Material_read_counter}.A2 = [];
            MATERIALS{Material_read_counter}.TREF = [];
            MATERIALS{Material_read_counter}.Xt = [];
            MATERIALS{Material_read_counter}.Xc = [];
            MATERIALS{Material_read_counter}.Yt = [];
            MATERIALS{Material_read_counter}.Yc = [];
            MATERIALS{Material_read_counter}.S = [];
            MATERIALS{Material_read_counter}.GE = [];
            MATERIALS{Material_read_counter}.F12 = [];
            MATERIALS{Material_read_counter}.STRN = [];

            % Чтение ID
            if (length(string) < 24)
                End_index = length(string);
            else
                End_index = 24;
            end
            tmp_number = str2double(string(9:End_index));
            if (isnan(tmp_number))
                fprintf (1, '\nНеправильная запись с номером материала в карточке MAT8* : [%s] !\n', strtrim(string(9:End_index)));
                ErrCode = -1;
                return;
            end
            MATERIALS{Material_read_counter}.ID = tmp_number;

            % проверка на записи в 10-ом поле
            if (deblank(string) > 72)
                fprintf (1, '\nОбнаружена запись в поле 10 первой строки карточки MAT8* %i !\n', MATERIALS{Material_read_counter}.ID);
                fprintf (1, 'Указатели продления карточки не поддерживаются в текущей версии программы.\n');
                fprintf (1, 'Обратетись к разработчику для решения этого вопроса.\n');
                ErrCode = -1;
                return;
            end

            % Чтение Patran_name
            if ((length(Previous_strings{1} ) >= 25) && strcmp(Previous_strings{1} (1:25), '$ Description of Material'))
                if ((length(Previous_strings{2} ) >= 20) && strcmp(Previous_strings{2} (1:19), '$ Material Record :')) % здесь нет ошибки с 20 и 19
                    MATERIALS{Material_read_counter}.Patran_name = strtrim(Previous_strings{2}(20:length(Previous_strings{2})));
                else
                    if ((length(Previous_strings{2} ) >= 3) && strcmp(Previous_strings{2}(1:2), '$ ')) % здесь нет ошибки с 3 и 2
                        if ((length(Previous_strings{3} ) >= 20) && strcmp(Previous_strings{3} (1:19), '$ Material Record :')) % здесь нет ошибки с 20 и 19
                            MATERIALS{Material_read_counter}.Patran_name = [strtrim(Previous_strings{3}(20:length(Previous_strings{3}))) strtrim(Previous_strings{2}(3:length(Previous_strings{2})))];
                        end
                    end
                end
            end

            % --- УНИВЕРСАЛЬНЫЙ АЛГОРИТМ ДЛЯ КАРТОЧЕК С ФИКСИРОВАННЫМ ЧИСЛОМ НЕОБЯЗАТЕЛЬНЫХ ПОЛЕЙ ---

            % Установка информационных параметров
            Card_name = 'MAT8*';
            Item_ID = MATERIALS{Material_read_counter}.ID;

            % Установка стартовых параметров
            Previous_index = 24;
            Record_length = 16;
            Next_string_large = 1;
            Record_read_counter = 1; % Отсчет прочтенных полей начинается со второго поля карточки
            End_card_flag = 0;

            % Чтение полей карточки
            Results_array = cell(18,1);

            for i=1:size(Results_array,1)

                [Read_Result, Fin, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings] = Read_Field_RB(Fin, Card_name, Item_ID, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings);

                if (ErrCode < 0)
                    break;
                end

                Results_array{i} = Read_Result;

            end

            if (ErrCode < 0)
                return;
            end

            % --- E1 ---                    
            if (~isempty(Results_array{1}))
                MATERIALS{Material_read_counter}.E1 = Results_array{1};
            end

            % --- E2 ---
            if (~isempty(Results_array{2}))
                MATERIALS{Material_read_counter}.E2 = Results_array{2};
            end

            % --- NU12 ---
            if (~isempty(Results_array{3}))
                MATERIALS{Material_read_counter}.NU12 = Results_array{3};
            end

            % --- G12 ---
            if (~isempty(Results_array{4}))
                MATERIALS{Material_read_counter}.G12 = Results_array{4};
            end

            % --- G1Z ---
            if (~isempty(Results_array{5}))
                MATERIALS{Material_read_counter}.G1Z = Results_array{5};
            end

            % --- G2Z ---
            if (~isempty(Results_array{6}))
                MATERIALS{Material_read_counter}.G2Z = Results_array{6};
            end

            % --- RHO ---
            if (~isempty(Results_array{7}))
                MATERIALS{Material_read_counter}.RHO = Results_array{7};
            end

            % --- A1 ---
            if (~isempty(Results_array{8}))
                MATERIALS{Material_read_counter}.A1 = Results_array{8};
            end

            % --- A2 ---
            if (~isempty(Results_array{9}))
                MATERIALS{Material_read_counter}.A2 = Results_array{9};
            end

            % --- TREF ---
            if (~isempty(Results_array{10}))
                MATERIALS{Material_read_counter}.TREF = Results_array{10};
            end

            % --- Xt ---
            if (~isempty(Results_array{11}))
                MATERIALS{Material_read_counter}.Xt = Results_array{11};
            end

            % --- Xc ---
            if (~isempty(Results_array{12}))
                MATERIALS{Material_read_counter}.Xc = Results_array{12};
            end

            % --- Yt ---
            if (~isempty(Results_array{13}))
                MATERIALS{Material_read_counter}.Yt = Results_array{13};
            end

            % --- Yc ---
            if (~isempty(Results_array{14}))
                MATERIALS{Material_read_counter}.Yc = Results_array{14};
            end

            % --- S ---
            if (~isempty(Results_array{15}))
                MATERIALS{Material_read_counter}.S = Results_array{15};
            end

            % --- GE ---
            if (~isempty(Results_array{16}))
                MATERIALS{Material_read_counter}.GE = Results_array{16};
            end

            % --- F12 ---
            if (~isempty(Results_array{17}))
                MATERIALS{Material_read_counter}.F12 = Results_array{17};
            end

            % --- STRN ---
            if (~isempty(Results_array{18}))
                MATERIALS{Material_read_counter}.STRN = Results_array{18};
            end


            % Для вывода прогресса
            Counter = Counter + 1;
            Counter_2 = Counter_2 + 1;
            if (Counter_2 == N_rec_segm)
                Counter_2 = 0;
                Written_count = Written_count + 1;
                if (Written_count < Time_segments)
                    fprintf (1, '#');
                    pause (Progress_Bar_Delay);
                end
            end


            continue;

        end

        if ((length(string) >= 5) && strcmp(string(2:5), 'AT8,')) % FREE FIELD FORMAT

            fprintf (1, '\nОшибка функции Read_MATERIALS: FREE FIELD FORMAT файла bdf не поддерживается в текущей версии программы [MAT8,] !\n');
            ErrCode = -1;
            return; 

        end

        % Чтение MAT1

        if ((length(string) >= 5) && strcmp(string(2:5), 'AT1 ')) % SMALL FIELD FORMAT

            Material_read_counter = Material_read_counter + 1;

            MATERIALS{Material_read_counter}.card = 'MAT1';
            MATERIALS{Material_read_counter}.ID = [];
            MATERIALS{Material_read_counter}.Patran_name = [];
            MATERIALS{Material_read_counter}.E = [];
            MATERIALS{Material_read_counter}.G = [];
            MATERIALS{Material_read_counter}.NU = [];
            MATERIALS{Material_read_counter}.RHO = [];
            MATERIALS{Material_read_counter}.A = [];
            MATERIALS{Material_read_counter}.TREF = [];
            MATERIALS{Material_read_counter}.GE = [];
            MATERIALS{Material_read_counter}.ST = [];
            MATERIALS{Material_read_counter}.SC = [];
            MATERIALS{Material_read_counter}.SS = [];
            MATERIALS{Material_read_counter}.MCSID = [];
            
            % Чтение ID
            if (length(string) < 16)
                End_index = length(string);
            else
                End_index = 16;
            end
            tmp_number = str2double(string(9:End_index));
            if (isnan(tmp_number))
                fprintf (1, '\nНеправильная запись с номером материала в карточке MAT1 : [%s] !\n', strtrim(string(9:End_index)));
                ErrCode = -1;
                return;
            end
            MATERIALS{Material_read_counter}.ID = tmp_number;
            
            % проверка на записи в 10-ом поле
            if (deblank(string) > 72)
                fprintf (1, '\nОбнаружена запись в поле 10 первой строки карточки MAT1 %i !\n', MATERIALS{Material_read_counter}.ID);
                fprintf (1, 'Указатели продления карточки не поддерживаются в текущей версии программы.\n');
                fprintf (1, 'Обратетись к разработчику для решения этого вопроса.\n');
                ErrCode = -1;
                return;
            end
            
            % Чтение Patran_name
            if ((length(Previous_strings{1} ) >= 25) && strcmp(Previous_strings{1} (1:25), '$ Description of Material'))
                if ((length(Previous_strings{2} ) >= 20) && strcmp(Previous_strings{2} (1:19), '$ Material Record :')) % здесь нет ошибки с 20 и 19
                    MATERIALS{Material_read_counter}.Patran_name = strtrim(Previous_strings{2}(20:length(Previous_strings{2})));
                else
                    if ((length(Previous_strings{2} ) >= 3) && strcmp(Previous_strings{2}(1:2), '$ ')) % здесь нет ошибки с 3 и 2
                        if ((length(Previous_strings{3} ) >= 20) && strcmp(Previous_strings{3} (1:19), '$ Material Record :')) % здесь нет ошибки с 20 и 19
                            MATERIALS{Material_read_counter}.Patran_name = [strtrim(Previous_strings{3}(20:length(Previous_strings{3}))) strtrim(Previous_strings{2}(3:length(Previous_strings{2})))];
                        end
                    end
                end
            end
            
            % Чтение Patran_name в случае MATS1 - MAT1
            
            MATS1_find_flag = 0;
            
            if ((length(Previous_strings{1} ) >= 6) && strcmp(Previous_strings{1} (1:5), 'MATS1')) % здесь нет ошибки с 6 и 5
                MATS1_find_flag = 1;
                Index_shift = 0;
            end
            
            if ((length(Previous_strings{1} ) >= 1) && strcmp(Previous_strings{1}(1), '*'))
                if ((length(Previous_strings{2} ) >= 6) && strcmp(Previous_strings{2} (1:5), 'MATS1')) % здесь нет ошибки с 6 и 5
                    MATS1_find_flag = 1;
                    Index_shift = 1;
                end
            end
            
            if (MATS1_find_flag == 1)
                
                % Чтение ID материала MATS1
                switch Previous_strings{1+Index_shift}(6)
                    
                    case ' ' % SMALL FIELD FORMAT

                        tmp_number = str2double(Previous_strings{1+Index_shift}(9:16));
                        if (isnan(tmp_number))
                            fprintf (1, '\nНеправильная запись с номером материала в карточке MATS1, предшествующей карточке MAT1 : [%s] !\n', strtrim(Previous_strings{1+Index_shift}(9:16)));
                            ErrCode = -1;
                            return;
                        end
                        
                    case '*' % LARGE FIELD FORMAT

                        tmp_number = str2double(Previous_strings{1+Index_shift}(9:24));
                        if (isnan(tmp_number))
                            fprintf (1, '\nНеправильная запись с номером материала в карточке MATS1*, предшествующей карточке MAT1 : [%s] !\n', strtrim(Previous_strings{1+Index_shift}(9:24)));
                            ErrCode = -1;
                            return;
                        end
                        
                    otherwise

                        fprintf (1, '\nОшибка функции Read_MATERIALS: непредвиденное значение символа 6 в карточке [MATS1%s], предшествующей карточке MAT1 !\n', Previous_strings{1+Index_shift}(6));
                        ErrCode = -1;
                        return; 
                    
                end
                
                if (tmp_number == MATERIALS{Material_read_counter}.ID)
                
                    if ((length(Previous_strings{2+Index_shift} ) >= 25) && strcmp(Previous_strings{2+Index_shift} (1:25), '$ Description of Material'))

                        if ((length(Previous_strings{3+Index_shift} ) >= 20) && strcmp(Previous_strings{3+Index_shift} (1:19), '$ Material Record :')) % здесь нет ошибки с 20 и 19
                            MATERIALS{Material_read_counter}.Patran_name = strtrim(Previous_strings{3+Index_shift}(20:length(Previous_strings{3+Index_shift})));
                        else
                            if ((length(Previous_strings{3+Index_shift} ) >= 3) && strcmp(Previous_strings{3+Index_shift}(1:2), '$ ')) % здесь нет ошибки с 3 и 2
                                if ((length(Previous_strings{4+Index_shift} ) >= 20) && strcmp(Previous_strings{4+Index_shift} (1:19), '$ Material Record :')) % здесь нет ошибки с 20 и 19
                                    MATERIALS{Material_read_counter}.Patran_name = [strtrim(Previous_strings{4+Index_shift}(20:length(Previous_strings{4+Index_shift}))) strtrim(Previous_strings{3+Index_shift}(3:length(Previous_strings{3+Index_shift})))];
                                end
                            end
                        end
                        
                    end
                
                end
                
            end
            
            % --- УНИВЕРСАЛЬНЫЙ АЛГОРИТМ ДЛЯ КАРТОЧЕК С ФИКСИРОВАННЫМ ЧИСЛОМ НЕОБЯЗАТЕЛЬНЫХ ПОЛЕЙ ---

            % Установка информационных параметров
            Card_name = 'MAT1';
            Item_ID = MATERIALS{Material_read_counter}.ID;

            % Установка стартовых параметров
            Previous_index = 16;
            Record_length = 8;
            Next_string_large = 0;
            Record_read_counter = 1; % Отсчет прочтенных полей начинается со второго поля карточки
            End_card_flag = 0;
            
            % Чтение полей карточки
            Results_array = cell(10,1);

            for i=1:size(Results_array,1)

                [Read_Result, Fin, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings] = Read_Field_RB(Fin, Card_name, Item_ID, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings);

                if (ErrCode < 0)
                    break;
                end

                Results_array{i} = Read_Result;

            end

            if (ErrCode < 0)
                return;
            end

            % --- E ---
            if (~isempty(Results_array{1}))
                MATERIALS{Material_read_counter}.E = Results_array{1};
            end
            
            % --- G ---
            if (~isempty(Results_array{2}))
                MATERIALS{Material_read_counter}.G = Results_array{2};
            end
            
            % --- NU ---
            if (~isempty(Results_array{3}))
                MATERIALS{Material_read_counter}.NU = Results_array{3};
            end
            
            % --- RHO ---
            if (~isempty(Results_array{4}))
                MATERIALS{Material_read_counter}.RHO = Results_array{4};
            end
            
            % --- A ---
            if (~isempty(Results_array{5}))
                MATERIALS{Material_read_counter}.A = Results_array{5};
            end
            
            % --- TREF ---
            if (~isempty(Results_array{6}))
                MATERIALS{Material_read_counter}.TREF = Results_array{6};
            end
            
            % --- GE ---
            if (~isempty(Results_array{7}))
                MATERIALS{Material_read_counter}.GE = Results_array{7};
            end
            
            % --- ST ---
            if (~isempty(Results_array{8}))
                MATERIALS{Material_read_counter}.ST = Results_array{8};
            end
            
            % --- SC ---
            if (~isempty(Results_array{9}))
                MATERIALS{Material_read_counter}.SC = Results_array{9};
            end
            
            % --- SS ---
            if (~isempty(Results_array{10}))
                MATERIALS{Material_read_counter}.SS = Results_array{10};
            end
            
            % --- Чтение MCSID --- (integer! используется функция Read_Field_IB)
            
            [Read_Result, Fin, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings] = Read_Field_IB(Fin, Card_name, Item_ID, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings); %#ok<ASGLU>

            if (ErrCode < 0)
                return;
            end

            if (~isempty(Read_Result))
                MATERIALS{Material_read_counter}.MCSID = Read_Result;
            end            


            % Для вывода прогресса
            Counter = Counter + 1;
            Counter_2 = Counter_2 + 1;
            if (Counter_2 == N_rec_segm)
                Counter_2 = 0;
                Written_count = Written_count + 1;
                if (Written_count < Time_segments)
                    fprintf (1, '#');
                    pause (Progress_Bar_Delay);
                end
            end


            continue;

        end

        if ((length(string) >= 5) && strcmp(string(2:5), 'AT1*')) % LARGE FIELD FORMAT

            Material_read_counter = Material_read_counter + 1;

            MATERIALS{Material_read_counter}.card = 'MAT1';
            MATERIALS{Material_read_counter}.ID = [];
            MATERIALS{Material_read_counter}.Patran_name = [];
            MATERIALS{Material_read_counter}.E = [];
            MATERIALS{Material_read_counter}.G = [];
            MATERIALS{Material_read_counter}.NU = [];
            MATERIALS{Material_read_counter}.RHO = [];
            MATERIALS{Material_read_counter}.A = [];
            MATERIALS{Material_read_counter}.TREF = [];
            MATERIALS{Material_read_counter}.GE = [];
            MATERIALS{Material_read_counter}.ST = [];
            MATERIALS{Material_read_counter}.SC = [];
            MATERIALS{Material_read_counter}.SS = [];
            MATERIALS{Material_read_counter}.MCSID = [];
            
            % Чтение ID
            if (length(string) < 24)
                End_index = length(string);
            else
                End_index = 24;
            end
            tmp_number = str2double(string(9:End_index));
            if (isnan(tmp_number))
                fprintf (1, '\nНеправильная запись с номером материала в карточке MAT1* : [%s] !\n', strtrim(string(9:End_index)));
                ErrCode = -1;
                return;
            end
            MATERIALS{Material_read_counter}.ID = tmp_number;
            
            % проверка на записи в 10-ом поле
            if (deblank(string) > 72)
                fprintf (1, '\nОбнаружена запись в поле 10 первой строки карточки MAT1* %i !\n', MATERIALS{Material_read_counter}.ID);
                fprintf (1, 'Указатели продления карточки не поддерживаются в текущей версии программы.\n');
                fprintf (1, 'Обратетись к разработчику для решения этого вопроса.\n');
                ErrCode = -1;
                return;
            end
            
            % Чтение Patran_name
            if ((length(Previous_strings{1} ) >= 25) && strcmp(Previous_strings{1} (1:25), '$ Description of Material'))
                if ((length(Previous_strings{2} ) >= 20) && strcmp(Previous_strings{2} (1:19), '$ Material Record :')) % здесь нет ошибки с 20 и 19
                    MATERIALS{Material_read_counter}.Patran_name = strtrim(Previous_strings{2}(20:length(Previous_strings{2})));
                else
                    if ((length(Previous_strings{2} ) >= 3) && strcmp(Previous_strings{2}(1:2), '$ ')) % здесь нет ошибки с 3 и 2
                        if ((length(Previous_strings{3} ) >= 20) && strcmp(Previous_strings{3} (1:19), '$ Material Record :')) % здесь нет ошибки с 20 и 19
                            MATERIALS{Material_read_counter}.Patran_name = [strtrim(Previous_strings{3}(20:length(Previous_strings{3}))) strtrim(Previous_strings{2}(3:length(Previous_strings{2})))];
                        end
                    end
                end
            end
            
            % Чтение Patran_name в случае MATS1 - MAT1
            
            MATS1_find_flag = 0;
            
            if ((length(Previous_strings{1} ) >= 6) && strcmp(Previous_strings{1} (1:5), 'MATS1')) % здесь нет ошибки с 6 и 5
                MATS1_find_flag = 1;
                Index_shift = 0;
            end
            
            if ((length(Previous_strings{1} ) >= 1) && strcmp(Previous_strings{1}(1), '*'))
                if ((length(Previous_strings{2} ) >= 6) && strcmp(Previous_strings{2} (1:5), 'MATS1')) % здесь нет ошибки с 6 и 5
                    MATS1_find_flag = 1;
                    Index_shift = 1;
                end
            end
            
            if (MATS1_find_flag == 1)
                
                % Чтение ID материала MATS1
                switch Previous_strings{1+Index_shift}(6)
                    
                    case ' ' % SMALL FIELD FORMAT

                        tmp_number = str2double(Previous_strings{1+Index_shift}(9:16));
                        if (isnan(tmp_number))
                            fprintf (1, '\nНеправильная запись с номером материала в карточке MATS1, предшествующей карточке MAT1 : [%s] !\n', strtrim(Previous_strings{1+Index_shift}(9:16)));
                            ErrCode = -1;
                            return;
                        end
                        
                    case '*' % LARGE FIELD FORMAT

                        tmp_number = str2double(Previous_strings{1+Index_shift}(9:24));
                        if (isnan(tmp_number))
                            fprintf (1, '\nНеправильная запись с номером материала в карточке MATS1*, предшествующей карточке MAT1 : [%s] !\n', strtrim(Previous_strings{1+Index_shift}(9:24)));
                            ErrCode = -1;
                            return;
                        end
                        
                    otherwise

                        fprintf (1, '\nОшибка функции Read_MATERIALS: непредвиденное значение символа 6 в карточке [MATS1%s], предшествующей карточке MAT1 !\n', Previous_strings{1+Index_shift}(6));
                        ErrCode = -1;
                        return; 
                    
                end
                
                if (tmp_number == MATERIALS{Material_read_counter}.ID)
                
                    if ((length(Previous_strings{2+Index_shift} ) >= 25) && strcmp(Previous_strings{2+Index_shift} (1:25), '$ Description of Material'))

                        if ((length(Previous_strings{3+Index_shift} ) >= 20) && strcmp(Previous_strings{3+Index_shift} (1:19), '$ Material Record :')) % здесь нет ошибки с 20 и 19
                            MATERIALS{Material_read_counter}.Patran_name = strtrim(Previous_strings{3+Index_shift}(20:length(Previous_strings{3+Index_shift})));
                        else
                            if ((length(Previous_strings{3+Index_shift} ) >= 3) && strcmp(Previous_strings{3+Index_shift}(1:2), '$ ')) % здесь нет ошибки с 3 и 2
                                if ((length(Previous_strings{4+Index_shift} ) >= 20) && strcmp(Previous_strings{4+Index_shift} (1:19), '$ Material Record :')) % здесь нет ошибки с 20 и 19
                                    MATERIALS{Material_read_counter}.Patran_name = [strtrim(Previous_strings{4+Index_shift}(20:length(Previous_strings{4+Index_shift}))) strtrim(Previous_strings{3+Index_shift}(3:length(Previous_strings{3+Index_shift})))];
                                end
                            end
                        end

                    end
                
                end
                
            end
            
            % --- УНИВЕРСАЛЬНЫЙ АЛГОРИТМ ДЛЯ КАРТОЧЕК С ФИКСИРОВАННЫМ ЧИСЛОМ НЕОБЯЗАТЕЛЬНЫХ ПОЛЕЙ ---

            % Установка информационных параметров
            Card_name = 'MAT1*';
            Item_ID = MATERIALS{Material_read_counter}.ID;

            % Установка стартовых параметров
            Previous_index = 24;
            Record_length = 16;
            Next_string_large = 1;
            Record_read_counter = 1; % Отсчет прочтенных полей начинается со второго поля карточки
            End_card_flag = 0;

            % Чтение полей карточки
            Results_array = cell(10,1);

            for i=1:size(Results_array,1)

                [Read_Result, Fin, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings] = Read_Field_RB(Fin, Card_name, Item_ID, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings);

                if (ErrCode < 0)
                    break;
                end

                Results_array{i} = Read_Result;

            end

            if (ErrCode < 0)
                return;
            end

            % --- E ---
            if (~isempty(Results_array{1}))
                MATERIALS{Material_read_counter}.E = Results_array{1};
            end
            
            % --- G ---
            if (~isempty(Results_array{2}))
                MATERIALS{Material_read_counter}.G = Results_array{2};
            end
            
            % --- NU ---
            if (~isempty(Results_array{3}))
                MATERIALS{Material_read_counter}.NU = Results_array{3};
            end
            
            % --- RHO ---
            if (~isempty(Results_array{4}))
                MATERIALS{Material_read_counter}.RHO = Results_array{4};
            end
            
            % --- A ---
            if (~isempty(Results_array{5}))
                MATERIALS{Material_read_counter}.A = Results_array{5};
            end
            
            % --- TREF ---
            if (~isempty(Results_array{6}))
                MATERIALS{Material_read_counter}.TREF = Results_array{6};
            end
            
            % --- GE ---
            if (~isempty(Results_array{7}))
                MATERIALS{Material_read_counter}.GE = Results_array{7};
            end
            
            % --- ST ---
            if (~isempty(Results_array{8}))
                MATERIALS{Material_read_counter}.ST = Results_array{8};
            end
            
            % --- SC ---
            if (~isempty(Results_array{9}))
                MATERIALS{Material_read_counter}.SC = Results_array{9};
            end
            
            % --- SS ---
            if (~isempty(Results_array{10}))
                MATERIALS{Material_read_counter}.SS = Results_array{10};
            end
            
            % --- Чтение MCSID --- (integer! используется функция Read_Field_IB)
            
            [Read_Result, Fin, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings] = Read_Field_IB(Fin, Card_name, Item_ID, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings); %#ok<ASGLU>

            if (ErrCode < 0)
                return;
            end

            if (~isempty(Read_Result))
                MATERIALS{Material_read_counter}.MCSID = Read_Result;
            end
            

            % Для вывода прогресса
            Counter = Counter + 1;
            Counter_2 = Counter_2 + 1;
            if (Counter_2 == N_rec_segm)
                Counter_2 = 0;
                Written_count = Written_count + 1;
                if (Written_count < Time_segments)
                    fprintf (1, '#');
                    pause (Progress_Bar_Delay);
                end
            end


            continue;

        end

        if ((length(string) >= 5) && strcmp(string(2:5), 'AT1,')) % FREE FIELD FORMAT

            fprintf (1, '\nОшибка функции Read_MATERIALS: FREE FIELD FORMAT файла bdf не поддерживается в текущей версии программы [MAT1,] !\n');
            ErrCode = -1;
            return; 

        end
        
        % Чтение MAT9

        if ((length(string) >= 5) && strcmp(string(2:5), 'AT9 ')) % SMALL FIELD FORMAT

            Material_read_counter = Material_read_counter + 1;

            MATERIALS{Material_read_counter}.card = 'MAT9';
            MATERIALS{Material_read_counter}.ID = [];
            MATERIALS{Material_read_counter}.Patran_name = [];
            MATERIALS{Material_read_counter}.G11 = 0;
            MATERIALS{Material_read_counter}.G12 = 0;
            MATERIALS{Material_read_counter}.G13 = 0;
            MATERIALS{Material_read_counter}.G14 = 0;
            MATERIALS{Material_read_counter}.G15 = 0;
            MATERIALS{Material_read_counter}.G16 = 0;
            MATERIALS{Material_read_counter}.G22 = 0;
            MATERIALS{Material_read_counter}.G23 = 0;
            MATERIALS{Material_read_counter}.G24 = 0;
            MATERIALS{Material_read_counter}.G25 = 0;
            MATERIALS{Material_read_counter}.G26 = 0;
            MATERIALS{Material_read_counter}.G33 = 0;
            MATERIALS{Material_read_counter}.G34 = 0;
            MATERIALS{Material_read_counter}.G35 = 0;
            MATERIALS{Material_read_counter}.G36 = 0;
            MATERIALS{Material_read_counter}.G44 = 0;
            MATERIALS{Material_read_counter}.G45 = 0;
            MATERIALS{Material_read_counter}.G46 = 0;
            MATERIALS{Material_read_counter}.G55 = 0;
            MATERIALS{Material_read_counter}.G56 = 0;
            MATERIALS{Material_read_counter}.G66 = 0;
            MATERIALS{Material_read_counter}.RHO = [];
            MATERIALS{Material_read_counter}.A1 = [];
            MATERIALS{Material_read_counter}.A2 = [];
            MATERIALS{Material_read_counter}.A3 = [];
            MATERIALS{Material_read_counter}.A4 = [];
            MATERIALS{Material_read_counter}.A5 = [];
            MATERIALS{Material_read_counter}.A6 = [];
            MATERIALS{Material_read_counter}.TREF = [];
            MATERIALS{Material_read_counter}.GE = [];
            MATERIALS{Material_read_counter}.GE11 = [];
            MATERIALS{Material_read_counter}.GE12 = [];
            MATERIALS{Material_read_counter}.GE13 = [];
            MATERIALS{Material_read_counter}.GE14 = [];
            MATERIALS{Material_read_counter}.GE15 = [];
            MATERIALS{Material_read_counter}.GE16 = [];
            MATERIALS{Material_read_counter}.GE22 = [];
            MATERIALS{Material_read_counter}.GE23 = [];
            MATERIALS{Material_read_counter}.GE24 = [];
            MATERIALS{Material_read_counter}.GE25 = [];
            MATERIALS{Material_read_counter}.GE26 = [];
            MATERIALS{Material_read_counter}.GE33 = [];
            MATERIALS{Material_read_counter}.GE34 = [];
            MATERIALS{Material_read_counter}.GE35 = [];
            MATERIALS{Material_read_counter}.GE36 = [];
            MATERIALS{Material_read_counter}.GE44 = [];
            MATERIALS{Material_read_counter}.GE45 = [];
            MATERIALS{Material_read_counter}.GE46 = [];
            MATERIALS{Material_read_counter}.GE55 = [];
            MATERIALS{Material_read_counter}.GE56 = [];
            MATERIALS{Material_read_counter}.GE66 = [];

            % Чтение ID
            if (length(string) < 16)
                End_index = length(string);
            else
                End_index = 16;
            end
            tmp_number = str2double(string(9:End_index));
            if (isnan(tmp_number))
                fprintf (1, '\nНеправильная запись с номером материала в карточке MAT9 : [%s] !\n', strtrim(string(9:End_index)));
                ErrCode = -1;
                return;
            end
            MATERIALS{Material_read_counter}.ID = tmp_number;

            % проверка на записи в 10-ом поле
            if (deblank(string) > 72)
                fprintf (1, '\nОбнаружена запись в поле 10 первой строки карточки MAT9 %i !\n', MATERIALS{Material_read_counter}.ID);
                fprintf (1, 'Указатели продления карточки не поддерживаются в текущей версии программы.\n');
                fprintf (1, 'Обратетись к разработчику для решения этого вопроса.\n');
                ErrCode = -1;
                return;
            end

            % Чтение Patran_name
            if ((length(Previous_strings{1} ) >= 25) && strcmp(Previous_strings{1} (1:25), '$ Description of Material'))
                if ((length(Previous_strings{2} ) >= 20) && strcmp(Previous_strings{2} (1:19), '$ Material Record :')) % здесь нет ошибки с 20 и 19
                    MATERIALS{Material_read_counter}.Patran_name = strtrim(Previous_strings{2}(20:length(Previous_strings{2})));
                else
                    if ((length(Previous_strings{2} ) >= 3) && strcmp(Previous_strings{2}(1:2), '$ ')) % здесь нет ошибки с 3 и 2
                        if ((length(Previous_strings{3} ) >= 20) && strcmp(Previous_strings{3} (1:19), '$ Material Record :')) % здесь нет ошибки с 20 и 19
                            MATERIALS{Material_read_counter}.Patran_name = [strtrim(Previous_strings{3}(20:length(Previous_strings{3}))) strtrim(Previous_strings{2}(3:length(Previous_strings{2})))];
                        end
                    end
                end
            end

            % --- УНИВЕРСАЛЬНЫЙ АЛГОРИТМ ДЛЯ КАРТОЧЕК С ФИКСИРОВАННЫМ ЧИСЛОМ НЕОБЯЗАТЕЛЬНЫХ ПОЛЕЙ ---

            % Установка информационных параметров
            Card_name = 'MAT9';
            Item_ID = MATERIALS{Material_read_counter}.ID;

            % Установка стартовых параметров
            Previous_index = 16;
            Record_length = 8;
            Next_string_large = 0;
            Record_read_counter = 1; % Отсчет прочтенных полей начинается со второго поля карточки
            End_card_flag = 0;

            % Чтение полей карточки
            Results_array = cell(30,1);

            for i=1:size(Results_array,1)

                [Read_Result, Fin, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings] = Read_Field_RB(Fin, Card_name, Item_ID, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings);

                if (ErrCode < 0)
                    break;
                end

                Results_array{i} = Read_Result;

            end

            if (ErrCode < 0)
                return;
            end

            % --- G11 ---
            if (~isempty(Results_array{1}))
                MATERIALS{Material_read_counter}.G11 = Results_array{1};
            end

            % --- G12 ---
            if (~isempty(Results_array{2}))
                MATERIALS{Material_read_counter}.G12 = Results_array{2};
            end

            % --- G13 ---
            if (~isempty(Results_array{3}))
                MATERIALS{Material_read_counter}.G13 = Results_array{3};
            end

            % --- G14 ---
            if (~isempty(Results_array{4}))
                MATERIALS{Material_read_counter}.G14 = Results_array{4};
            end

            % --- G15 ---
            if (~isempty(Results_array{5}))
                MATERIALS{Material_read_counter}.G15 = Results_array{5};
            end

            % --- G16 ---
            if (~isempty(Results_array{6}))
                MATERIALS{Material_read_counter}.G16 = Results_array{6};
            end

            % --- G22 ---
            if (~isempty(Results_array{7}))
                MATERIALS{Material_read_counter}.G22 = Results_array{7};
            end

            % --- G23 ---
            if (~isempty(Results_array{8}))
                MATERIALS{Material_read_counter}.G23 = Results_array{8};
            end

            % --- G24 ---
            if (~isempty(Results_array{9}))
                MATERIALS{Material_read_counter}.G24 = Results_array{9};
            end

            % --- G25 ---
            if (~isempty(Results_array{10}))
                MATERIALS{Material_read_counter}.G25 = Results_array{10};
            end

            % --- G26 ---
            if (~isempty(Results_array{11}))
                MATERIALS{Material_read_counter}.G26 = Results_array{11};
            end

            % --- G33 ---
            if (~isempty(Results_array{12}))
                MATERIALS{Material_read_counter}.G33 = Results_array{12};
            end

            % --- G34 ---
            if (~isempty(Results_array{13}))
                MATERIALS{Material_read_counter}.G34 = Results_array{13};
            end

            % --- G35 ---
            if (~isempty(Results_array{14}))
                MATERIALS{Material_read_counter}.G35 = Results_array{14};
            end

            % --- G36 ---
            if (~isempty(Results_array{15}))
                MATERIALS{Material_read_counter}.G36 = Results_array{15};
            end

            % --- G44 ---
            if (~isempty(Results_array{16}))
                MATERIALS{Material_read_counter}.G44 = Results_array{16};
            end

            % --- G45 ---
            if (~isempty(Results_array{17}))
                MATERIALS{Material_read_counter}.G45 = Results_array{17};
            end

            % --- G46 ---
            if (~isempty(Results_array{18}))
                MATERIALS{Material_read_counter}.G46 = Results_array{18};
            end

            % --- G55 ---
            if (~isempty(Results_array{19}))
                MATERIALS{Material_read_counter}.G55 = Results_array{19};
            end

            % --- G56 ---
            if (~isempty(Results_array{20}))
                MATERIALS{Material_read_counter}.G56 = Results_array{20};
            end

            % --- G66 ---
            if (~isempty(Results_array{21}))
                MATERIALS{Material_read_counter}.G66 = Results_array{21};
            end

            % --- RHO ---
            if (~isempty(Results_array{22}))
                MATERIALS{Material_read_counter}.RHO = Results_array{22};
            end

            % --- A1 ---
            if (~isempty(Results_array{23}))
                MATERIALS{Material_read_counter}.A1 = Results_array{23};
            end

            % --- A2 ---
            if (~isempty(Results_array{24}))
                MATERIALS{Material_read_counter}.A2 = Results_array{24};
            end

            % --- A3 ---
            if (~isempty(Results_array{25}))
                MATERIALS{Material_read_counter}.A3 = Results_array{25};
            end

            % --- A4 ---
            if (~isempty(Results_array{26}))
                MATERIALS{Material_read_counter}.A4 = Results_array{26};
            end

            % --- A5 ---
            if (~isempty(Results_array{27}))
                MATERIALS{Material_read_counter}.A5 = Results_array{27};
            end

            % --- A6 ---
            if (~isempty(Results_array{28}))
                MATERIALS{Material_read_counter}.A6 = Results_array{28};
            end

            % --- TREF ---
            if (~isempty(Results_array{29}))
                MATERIALS{Material_read_counter}.TREF = Results_array{29};
            end

            % --- GE ---
            if (~isempty(Results_array{30}))
                MATERIALS{Material_read_counter}.GE = Results_array{30};
            end
            
            % Пропуск пустого поля

            [Fin, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings] = Read_Field_Skip(Fin, Card_name, Item_ID, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings);

            if (ErrCode < 0)
                return;
            end
            
            % Чтение полей карточки
            Results_array = cell(21,1);

            for i=1:size(Results_array,1)

                [Read_Result, Fin, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings] = Read_Field_RB(Fin, Card_name, Item_ID, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings);

                if (ErrCode < 0)
                    break;
                end

                Results_array{i} = Read_Result;

            end

            if (ErrCode < 0)
                return;
            end

            % --- GE11 ---
            if (~isempty(Results_array{1}))
                MATERIALS{Material_read_counter}.GE11 = Results_array{1};
            end

            % --- GE12 ---
            if (~isempty(Results_array{2}))
                MATERIALS{Material_read_counter}.GE12 = Results_array{2};
            end

            % --- GE13 ---
            if (~isempty(Results_array{3}))
                MATERIALS{Material_read_counter}.GE13 = Results_array{3};
            end

            % --- GE14 ---
            if (~isempty(Results_array{4}))
                MATERIALS{Material_read_counter}.GE14 = Results_array{4};
            end

            % --- GE15 ---
            if (~isempty(Results_array{5}))
                MATERIALS{Material_read_counter}.GE15 = Results_array{5};
            end

            % --- GE16 ---
            if (~isempty(Results_array{6}))
                MATERIALS{Material_read_counter}.GE16 = Results_array{6};
            end

            % --- GE22 ---
            if (~isempty(Results_array{7}))
                MATERIALS{Material_read_counter}.GE22 = Results_array{7};
            end

            % --- GE23 ---
            if (~isempty(Results_array{8}))
                MATERIALS{Material_read_counter}.GE23 = Results_array{8};
            end

            % --- GE24 ---
            if (~isempty(Results_array{9}))
                MATERIALS{Material_read_counter}.GE24 = Results_array{9};
            end

            % --- GE25 ---
            if (~isempty(Results_array{10}))
                MATERIALS{Material_read_counter}.GE25 = Results_array{10};
            end

            % --- GE26 ---
            if (~isempty(Results_array{11}))
                MATERIALS{Material_read_counter}.GE26 = Results_array{11};
            end

            % --- GE33 ---
            if (~isempty(Results_array{12}))
                MATERIALS{Material_read_counter}.GE33 = Results_array{12};
            end

            % --- GE34 ---
            if (~isempty(Results_array{13}))
                MATERIALS{Material_read_counter}.GE34 = Results_array{13};
            end

            % --- GE35 ---
            if (~isempty(Results_array{14}))
                MATERIALS{Material_read_counter}.GE35 = Results_array{14};
            end

            % --- GE36 ---
            if (~isempty(Results_array{15}))
                MATERIALS{Material_read_counter}.GE36 = Results_array{15};
            end

            % --- GE44 ---
            if (~isempty(Results_array{16}))
                MATERIALS{Material_read_counter}.GE44 = Results_array{16};
            end

            % --- GE45 ---
            if (~isempty(Results_array{17}))
                MATERIALS{Material_read_counter}.GE45 = Results_array{17};
            end

            % --- GE46 ---
            if (~isempty(Results_array{18}))
                MATERIALS{Material_read_counter}.GE46 = Results_array{18};
            end

            % --- GE55 ---
            if (~isempty(Results_array{19}))
                MATERIALS{Material_read_counter}.GE55 = Results_array{19};
            end

            % --- GE56 ---
            if (~isempty(Results_array{20}))
                MATERIALS{Material_read_counter}.GE56 = Results_array{20};
            end

            % --- GE66 ---
            if (~isempty(Results_array{21}))
                MATERIALS{Material_read_counter}.GE66 = Results_array{21};
            end


            % Для вывода прогресса
            Counter = Counter + 1;
            Counter_2 = Counter_2 + 1;
            if (Counter_2 == N_rec_segm)
                Counter_2 = 0;
                Written_count = Written_count + 1;
                if (Written_count < Time_segments)
                    fprintf (1, '#');
                    pause (Progress_Bar_Delay);
                end
            end


            continue;

        end

        if ((length(string) >= 5) && strcmp(string(2:5), 'AT9*')) % LARGE FIELD FORMAT

            Material_read_counter = Material_read_counter + 1;

            MATERIALS{Material_read_counter}.card = 'MAT9';
            MATERIALS{Material_read_counter}.ID = [];
            MATERIALS{Material_read_counter}.Patran_name = [];
            MATERIALS{Material_read_counter}.G11 = 0;
            MATERIALS{Material_read_counter}.G12 = 0;
            MATERIALS{Material_read_counter}.G13 = 0;
            MATERIALS{Material_read_counter}.G14 = 0;
            MATERIALS{Material_read_counter}.G15 = 0;
            MATERIALS{Material_read_counter}.G16 = 0;
            MATERIALS{Material_read_counter}.G22 = 0;
            MATERIALS{Material_read_counter}.G23 = 0;
            MATERIALS{Material_read_counter}.G24 = 0;
            MATERIALS{Material_read_counter}.G25 = 0;
            MATERIALS{Material_read_counter}.G26 = 0;
            MATERIALS{Material_read_counter}.G33 = 0;
            MATERIALS{Material_read_counter}.G34 = 0;
            MATERIALS{Material_read_counter}.G35 = 0;
            MATERIALS{Material_read_counter}.G36 = 0;
            MATERIALS{Material_read_counter}.G44 = 0;
            MATERIALS{Material_read_counter}.G45 = 0;
            MATERIALS{Material_read_counter}.G46 = 0;
            MATERIALS{Material_read_counter}.G55 = 0;
            MATERIALS{Material_read_counter}.G56 = 0;
            MATERIALS{Material_read_counter}.G66 = 0;
            MATERIALS{Material_read_counter}.RHO = [];
            MATERIALS{Material_read_counter}.A1 = [];
            MATERIALS{Material_read_counter}.A2 = [];
            MATERIALS{Material_read_counter}.A3 = [];
            MATERIALS{Material_read_counter}.A4 = [];
            MATERIALS{Material_read_counter}.A5 = [];
            MATERIALS{Material_read_counter}.A6 = [];
            MATERIALS{Material_read_counter}.TREF = [];
            MATERIALS{Material_read_counter}.GE = [];
            MATERIALS{Material_read_counter}.GE11 = [];
            MATERIALS{Material_read_counter}.GE12 = [];
            MATERIALS{Material_read_counter}.GE13 = [];
            MATERIALS{Material_read_counter}.GE14 = [];
            MATERIALS{Material_read_counter}.GE15 = [];
            MATERIALS{Material_read_counter}.GE16 = [];
            MATERIALS{Material_read_counter}.GE22 = [];
            MATERIALS{Material_read_counter}.GE23 = [];
            MATERIALS{Material_read_counter}.GE24 = [];
            MATERIALS{Material_read_counter}.GE25 = [];
            MATERIALS{Material_read_counter}.GE26 = [];
            MATERIALS{Material_read_counter}.GE33 = [];
            MATERIALS{Material_read_counter}.GE34 = [];
            MATERIALS{Material_read_counter}.GE35 = [];
            MATERIALS{Material_read_counter}.GE36 = [];
            MATERIALS{Material_read_counter}.GE44 = [];
            MATERIALS{Material_read_counter}.GE45 = [];
            MATERIALS{Material_read_counter}.GE46 = [];
            MATERIALS{Material_read_counter}.GE55 = [];
            MATERIALS{Material_read_counter}.GE56 = [];
            MATERIALS{Material_read_counter}.GE66 = [];

            % Чтение ID
            if (length(string) < 24)
                End_index = length(string);
            else
                End_index = 24;
            end
            tmp_number = str2double(string(9:End_index));
            if (isnan(tmp_number))
                fprintf (1, '\nНеправильная запись с номером материала в карточке MAT9* : [%s] !\n', strtrim(string(9:End_index)));
                ErrCode = -1;
                return;
            end
            MATERIALS{Material_read_counter}.ID = tmp_number;

            % проверка на записи в 10-ом поле
            if (deblank(string) > 72)
                fprintf (1, '\nОбнаружена запись в поле 10 первой строки карточки MAT9* %i !\n', MATERIALS{Material_read_counter}.ID);
                fprintf (1, 'Указатели продления карточки не поддерживаются в текущей версии программы.\n');
                fprintf (1, 'Обратетись к разработчику для решения этого вопроса.\n');
                ErrCode = -1;
                return;
            end

            % Чтение Patran_name
            if ((length(Previous_strings{1} ) >= 25) && strcmp(Previous_strings{1} (1:25), '$ Description of Material'))
                if ((length(Previous_strings{2} ) >= 20) && strcmp(Previous_strings{2} (1:19), '$ Material Record :')) % здесь нет ошибки с 20 и 19
                    MATERIALS{Material_read_counter}.Patran_name = strtrim(Previous_strings{2}(20:length(Previous_strings{2})));
                else
                    if ((length(Previous_strings{2} ) >= 3) && strcmp(Previous_strings{2}(1:2), '$ ')) % здесь нет ошибки с 3 и 2
                        if ((length(Previous_strings{3} ) >= 20) && strcmp(Previous_strings{3} (1:19), '$ Material Record :')) % здесь нет ошибки с 20 и 19
                            MATERIALS{Material_read_counter}.Patran_name = [strtrim(Previous_strings{3}(20:length(Previous_strings{3}))) strtrim(Previous_strings{2}(3:length(Previous_strings{2})))];
                        end
                    end
                end
            end

            % --- УНИВЕРСАЛЬНЫЙ АЛГОРИТМ ДЛЯ КАРТОЧЕК С ФИКСИРОВАННЫМ ЧИСЛОМ НЕОБЯЗАТЕЛЬНЫХ ПОЛЕЙ ---

            % Установка информационных параметров
            Card_name = 'MAT9*';
            Item_ID = MATERIALS{Material_read_counter}.ID;

            % Установка стартовых параметров
            Previous_index = 24;
            Record_length = 16;
            Next_string_large = 1;
            Record_read_counter = 1; % Отсчет прочтенных полей начинается со второго поля карточки
            End_card_flag = 0;

            % Чтение полей карточки
            Results_array = cell(30,1);

            for i=1:size(Results_array,1)

                [Read_Result, Fin, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings] = Read_Field_RB(Fin, Card_name, Item_ID, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings);

                if (ErrCode < 0)
                    break;
                end

                Results_array{i} = Read_Result;

            end

            if (ErrCode < 0)
                return;
            end

            % --- G11 ---
            if (~isempty(Results_array{1}))
                MATERIALS{Material_read_counter}.G11 = Results_array{1};
            end

            % --- G12 ---
            if (~isempty(Results_array{2}))
                MATERIALS{Material_read_counter}.G12 = Results_array{2};
            end

            % --- G13 ---
            if (~isempty(Results_array{3}))
                MATERIALS{Material_read_counter}.G13 = Results_array{3};
            end

            % --- G14 ---
            if (~isempty(Results_array{4}))
                MATERIALS{Material_read_counter}.G14 = Results_array{4};
            end

            % --- G15 ---
            if (~isempty(Results_array{5}))
                MATERIALS{Material_read_counter}.G15 = Results_array{5};
            end

            % --- G16 ---
            if (~isempty(Results_array{6}))
                MATERIALS{Material_read_counter}.G16 = Results_array{6};
            end

            % --- G22 ---
            if (~isempty(Results_array{7}))
                MATERIALS{Material_read_counter}.G22 = Results_array{7};
            end

            % --- G23 ---
            if (~isempty(Results_array{8}))
                MATERIALS{Material_read_counter}.G23 = Results_array{8};
            end

            % --- G24 ---
            if (~isempty(Results_array{9}))
                MATERIALS{Material_read_counter}.G24 = Results_array{9};
            end

            % --- G25 ---
            if (~isempty(Results_array{10}))
                MATERIALS{Material_read_counter}.G25 = Results_array{10};
            end

            % --- G26 ---
            if (~isempty(Results_array{11}))
                MATERIALS{Material_read_counter}.G26 = Results_array{11};
            end

            % --- G33 ---
            if (~isempty(Results_array{12}))
                MATERIALS{Material_read_counter}.G33 = Results_array{12};
            end

            % --- G34 ---
            if (~isempty(Results_array{13}))
                MATERIALS{Material_read_counter}.G34 = Results_array{13};
            end

            % --- G35 ---
            if (~isempty(Results_array{14}))
                MATERIALS{Material_read_counter}.G35 = Results_array{14};
            end

            % --- G36 ---
            if (~isempty(Results_array{15}))
                MATERIALS{Material_read_counter}.G36 = Results_array{15};
            end

            % --- G44 ---
            if (~isempty(Results_array{16}))
                MATERIALS{Material_read_counter}.G44 = Results_array{16};
            end

            % --- G45 ---
            if (~isempty(Results_array{17}))
                MATERIALS{Material_read_counter}.G45 = Results_array{17};
            end

            % --- G46 ---
            if (~isempty(Results_array{18}))
                MATERIALS{Material_read_counter}.G46 = Results_array{18};
            end

            % --- G55 ---
            if (~isempty(Results_array{19}))
                MATERIALS{Material_read_counter}.G55 = Results_array{19};
            end

            % --- G56 ---
            if (~isempty(Results_array{20}))
                MATERIALS{Material_read_counter}.G56 = Results_array{20};
            end

            % --- G66 ---
            if (~isempty(Results_array{21}))
                MATERIALS{Material_read_counter}.G66 = Results_array{21};
            end

            % --- RHO ---
            if (~isempty(Results_array{22}))
                MATERIALS{Material_read_counter}.RHO = Results_array{22};
            end

            % --- A1 ---
            if (~isempty(Results_array{23}))
                MATERIALS{Material_read_counter}.A1 = Results_array{23};
            end

            % --- A2 ---
            if (~isempty(Results_array{24}))
                MATERIALS{Material_read_counter}.A2 = Results_array{24};
            end

            % --- A3 ---
            if (~isempty(Results_array{25}))
                MATERIALS{Material_read_counter}.A3 = Results_array{25};
            end

            % --- A4 ---
            if (~isempty(Results_array{26}))
                MATERIALS{Material_read_counter}.A4 = Results_array{26};
            end

            % --- A5 ---
            if (~isempty(Results_array{27}))
                MATERIALS{Material_read_counter}.A5 = Results_array{27};
            end

            % --- A6 ---
            if (~isempty(Results_array{28}))
                MATERIALS{Material_read_counter}.A6 = Results_array{28};
            end

            % --- TREF ---
            if (~isempty(Results_array{29}))
                MATERIALS{Material_read_counter}.TREF = Results_array{29};
            end

            % --- GE ---
            if (~isempty(Results_array{30}))
                MATERIALS{Material_read_counter}.GE = Results_array{30};
            end
            
            % Пропуск пустого поля

            [Fin, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings] = Read_Field_Skip(Fin, Card_name, Item_ID, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings);

            if (ErrCode < 0)
                return;
            end
            
            % Чтение полей карточки
            Results_array = cell(21,1);

            for i=1:size(Results_array,1)

                [Read_Result, Fin, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings] = Read_Field_RB(Fin, Card_name, Item_ID, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings);

                if (ErrCode < 0)
                    break;
                end

                Results_array{i} = Read_Result;

            end

            if (ErrCode < 0)
                return;
            end

            % --- GE11 ---
            if (~isempty(Results_array{1}))
                MATERIALS{Material_read_counter}.GE11 = Results_array{1};
            end

            % --- GE12 ---
            if (~isempty(Results_array{2}))
                MATERIALS{Material_read_counter}.GE12 = Results_array{2};
            end

            % --- GE13 ---
            if (~isempty(Results_array{3}))
                MATERIALS{Material_read_counter}.GE13 = Results_array{3};
            end

            % --- GE14 ---
            if (~isempty(Results_array{4}))
                MATERIALS{Material_read_counter}.GE14 = Results_array{4};
            end

            % --- GE15 ---
            if (~isempty(Results_array{5}))
                MATERIALS{Material_read_counter}.GE15 = Results_array{5};
            end

            % --- GE16 ---
            if (~isempty(Results_array{6}))
                MATERIALS{Material_read_counter}.GE16 = Results_array{6};
            end

            % --- GE22 ---
            if (~isempty(Results_array{7}))
                MATERIALS{Material_read_counter}.GE22 = Results_array{7};
            end

            % --- GE23 ---
            if (~isempty(Results_array{8}))
                MATERIALS{Material_read_counter}.GE23 = Results_array{8};
            end

            % --- GE24 ---
            if (~isempty(Results_array{9}))
                MATERIALS{Material_read_counter}.GE24 = Results_array{9};
            end

            % --- GE25 ---
            if (~isempty(Results_array{10}))
                MATERIALS{Material_read_counter}.GE25 = Results_array{10};
            end

            % --- GE26 ---
            if (~isempty(Results_array{11}))
                MATERIALS{Material_read_counter}.GE26 = Results_array{11};
            end

            % --- GE33 ---
            if (~isempty(Results_array{12}))
                MATERIALS{Material_read_counter}.GE33 = Results_array{12};
            end

            % --- GE34 ---
            if (~isempty(Results_array{13}))
                MATERIALS{Material_read_counter}.GE34 = Results_array{13};
            end

            % --- GE35 ---
            if (~isempty(Results_array{14}))
                MATERIALS{Material_read_counter}.GE35 = Results_array{14};
            end

            % --- GE36 ---
            if (~isempty(Results_array{15}))
                MATERIALS{Material_read_counter}.GE36 = Results_array{15};
            end

            % --- GE44 ---
            if (~isempty(Results_array{16}))
                MATERIALS{Material_read_counter}.GE44 = Results_array{16};
            end

            % --- GE45 ---
            if (~isempty(Results_array{17}))
                MATERIALS{Material_read_counter}.GE45 = Results_array{17};
            end

            % --- GE46 ---
            if (~isempty(Results_array{18}))
                MATERIALS{Material_read_counter}.GE46 = Results_array{18};
            end

            % --- GE55 ---
            if (~isempty(Results_array{19}))
                MATERIALS{Material_read_counter}.GE55 = Results_array{19};
            end

            % --- GE56 ---
            if (~isempty(Results_array{20}))
                MATERIALS{Material_read_counter}.GE56 = Results_array{20};
            end

            % --- GE66 ---
            if (~isempty(Results_array{21}))
                MATERIALS{Material_read_counter}.GE66 = Results_array{21};
            end


            % Для вывода прогресса
            Counter = Counter + 1;
            Counter_2 = Counter_2 + 1;
            if (Counter_2 == N_rec_segm)
                Counter_2 = 0;
                Written_count = Written_count + 1;
                if (Written_count < Time_segments)
                    fprintf (1, '#');
                    pause (Progress_Bar_Delay);
                end
            end


            continue;

        end

        if ((length(string) >= 5) && strcmp(string(2:5), 'AT9,')) % FREE FIELD FORMAT

            fprintf (1, '\nОшибка функции Read_MATERIALS: FREE FIELD FORMAT файла bdf не поддерживается в текущей версии программы [MAT9,] !\n');
            ErrCode = -1;
            return; 

        end
        
        % Чтение MAT2

        if ((length(string) >= 5) && strcmp(string(2:5), 'AT2 ')) % SMALL FIELD FORMAT

            Material_read_counter = Material_read_counter + 1;

            MATERIALS{Material_read_counter}.card = 'MAT2';
            MATERIALS{Material_read_counter}.ID = [];
            MATERIALS{Material_read_counter}.Patran_name = [];
            MATERIALS{Material_read_counter}.G11 = 0;
            MATERIALS{Material_read_counter}.G12 = 0;
            MATERIALS{Material_read_counter}.G13 = 0;
            MATERIALS{Material_read_counter}.G22 = 0;
            MATERIALS{Material_read_counter}.G23 = 0;
            MATERIALS{Material_read_counter}.G33 = 0;
            MATERIALS{Material_read_counter}.RHO = [];
            MATERIALS{Material_read_counter}.A1 = [];
            MATERIALS{Material_read_counter}.A2 = [];
            MATERIALS{Material_read_counter}.A3 = [];
            MATERIALS{Material_read_counter}.TREF = [];
            MATERIALS{Material_read_counter}.GE = [];
            MATERIALS{Material_read_counter}.ST = [];
            MATERIALS{Material_read_counter}.SC = [];
            MATERIALS{Material_read_counter}.SS = [];
            MATERIALS{Material_read_counter}.MCSID = [];
            MATERIALS{Material_read_counter}.GE11 = [];
            MATERIALS{Material_read_counter}.GE12 = [];
            MATERIALS{Material_read_counter}.GE13 = [];
            MATERIALS{Material_read_counter}.GE22 = [];
            MATERIALS{Material_read_counter}.GE23 = [];
            MATERIALS{Material_read_counter}.GE33 = [];

            % Чтение ID
            if (length(string) < 16)
                End_index = length(string);
            else
                End_index = 16;
            end
            tmp_number = str2double(string(9:End_index));
            if (isnan(tmp_number))
                fprintf (1, '\nНеправильная запись с номером материала в карточке MAT2 : [%s] !\n', strtrim(string(9:End_index)));
                ErrCode = -1;
                return;
            end
            MATERIALS{Material_read_counter}.ID = tmp_number;

            % проверка на записи в 10-ом поле
            if (deblank(string) > 72)
                fprintf (1, '\nОбнаружена запись в поле 10 первой строки карточки MAT2 %i !\n', MATERIALS{Material_read_counter}.ID);
                fprintf (1, 'Указатели продления карточки не поддерживаются в текущей версии программы.\n');
                fprintf (1, 'Обратетись к разработчику для решения этого вопроса.\n');
                ErrCode = -1;
                return;
            end

            % Чтение Patran_name
            if ((length(Previous_strings{1} ) >= 25) && strcmp(Previous_strings{1} (1:25), '$ Description of Material'))
                if ((length(Previous_strings{2} ) >= 20) && strcmp(Previous_strings{2} (1:19), '$ Material Record :')) % здесь нет ошибки с 20 и 19
                    MATERIALS{Material_read_counter}.Patran_name = strtrim(Previous_strings{2}(20:length(Previous_strings{2})));
                else
                    if ((length(Previous_strings{2} ) >= 3) && strcmp(Previous_strings{2}(1:2), '$ ')) % здесь нет ошибки с 3 и 2
                        if ((length(Previous_strings{3} ) >= 20) && strcmp(Previous_strings{3} (1:19), '$ Material Record :')) % здесь нет ошибки с 20 и 19
                            MATERIALS{Material_read_counter}.Patran_name = [strtrim(Previous_strings{3}(20:length(Previous_strings{3}))) strtrim(Previous_strings{2}(3:length(Previous_strings{2})))];
                        end
                    end
                end
            end

            % --- УНИВЕРСАЛЬНЫЙ АЛГОРИТМ ДЛЯ КАРТОЧЕК С ФИКСИРОВАННЫМ ЧИСЛОМ НЕОБЯЗАТЕЛЬНЫХ ПОЛЕЙ ---

            % Установка информационных параметров
            Card_name = 'MAT2';
            Item_ID = MATERIALS{Material_read_counter}.ID;

            % Установка стартовых параметров
            Previous_index = 16;
            Record_length = 8;
            Next_string_large = 0;
            Record_read_counter = 1; % Отсчет прочтенных полей начинается со второго поля карточки
            End_card_flag = 0;

            % Чтение полей карточки
            Results_array = cell(15,1);

            for i=1:size(Results_array,1)

                [Read_Result, Fin, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings] = Read_Field_RB(Fin, Card_name, Item_ID, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings);

                if (ErrCode < 0)
                    break;
                end

                Results_array{i} = Read_Result;

            end

            if (ErrCode < 0)
                return;
            end

            % --- G11 ---
            if (~isempty(Results_array{1}))
                MATERIALS{Material_read_counter}.G11 = Results_array{1};
            end

            % --- G12 ---
            if (~isempty(Results_array{2}))
                MATERIALS{Material_read_counter}.G12 = Results_array{2};
            end

            % --- G13 ---
            if (~isempty(Results_array{3}))
                MATERIALS{Material_read_counter}.G13 = Results_array{3};
            end

            % --- G22 ---
            if (~isempty(Results_array{4}))
                MATERIALS{Material_read_counter}.G22 = Results_array{4};
            end

            % --- G23 ---
            if (~isempty(Results_array{5}))
                MATERIALS{Material_read_counter}.G23 = Results_array{5};
            end

            % --- G33 ---
            if (~isempty(Results_array{6}))
                MATERIALS{Material_read_counter}.G33 = Results_array{6};
            end

            % --- RHO ---
            if (~isempty(Results_array{7}))
                MATERIALS{Material_read_counter}.RHO = Results_array{7};
            end

            % --- A1 ---
            if (~isempty(Results_array{8}))
                MATERIALS{Material_read_counter}.A1 = Results_array{8};
            end

            % --- A2 ---
            if (~isempty(Results_array{9}))
                MATERIALS{Material_read_counter}.A2 = Results_array{9};
            end

            % --- A3 ---
            if (~isempty(Results_array{10}))
                MATERIALS{Material_read_counter}.A3 = Results_array{10};
            end

            % --- TREF ---
            if (~isempty(Results_array{11}))
                MATERIALS{Material_read_counter}.TREF = Results_array{11};
            end

            % --- GE ---
            if (~isempty(Results_array{12}))
                MATERIALS{Material_read_counter}.GE = Results_array{12};
            end

            % --- ST ---
            if (~isempty(Results_array{13}))
                MATERIALS{Material_read_counter}.ST = Results_array{13};
            end

            % --- SC ---
            if (~isempty(Results_array{14}))
                MATERIALS{Material_read_counter}.SC = Results_array{14};
            end

            % --- SS ---
            if (~isempty(Results_array{15}))
                MATERIALS{Material_read_counter}.SS = Results_array{15};
            end

            % --- Чтение MCSID --- (integer! используется функция Read_Field_IB)

            [Read_Result, Fin, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings] = Read_Field_IB(Fin, Card_name, Item_ID, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings);

            if (ErrCode < 0)
                return;
            end

            if (~isempty(Read_Result))
                MATERIALS{Material_read_counter}.MCSID = Read_Result;
            end
            
            % Чтение полей GEij
            Results_array = cell(6,1);

            for i=1:size(Results_array,1)

                [Read_Result, Fin, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings] = Read_Field_RB(Fin, Card_name, Item_ID, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings);

                if (ErrCode < 0)
                    break;
                end

                Results_array{i} = Read_Result;

            end

            if (ErrCode < 0)
                return;
            end

            % --- GE11 ---
            if (~isempty(Results_array{1}))
                MATERIALS{Material_read_counter}.GE11 = Results_array{1};
            end

            % --- GE12 ---
            if (~isempty(Results_array{2}))
                MATERIALS{Material_read_counter}.GE12 = Results_array{2};
            end

            % --- GE13 ---
            if (~isempty(Results_array{3}))
                MATERIALS{Material_read_counter}.GE13 = Results_array{3};
            end

            % --- GE22 ---
            if (~isempty(Results_array{4}))
                MATERIALS{Material_read_counter}.GE22 = Results_array{4};
            end

            % --- GE23 ---
            if (~isempty(Results_array{5}))
                MATERIALS{Material_read_counter}.GE23 = Results_array{5};
            end

            % --- GE33 ---
            if (~isempty(Results_array{6}))
                MATERIALS{Material_read_counter}.GE33 = Results_array{6};
            end


            % Для вывода прогресса
            Counter = Counter + 1;
            Counter_2 = Counter_2 + 1;
            if (Counter_2 == N_rec_segm)
                Counter_2 = 0;
                Written_count = Written_count + 1;
                if (Written_count < Time_segments)
                    fprintf (1, '#');
                    pause (Progress_Bar_Delay);
                end
            end


            continue;

        end

        if ((length(string) >= 5) && strcmp(string(2:5), 'AT2*')) % LARGE FIELD FORMAT

            Material_read_counter = Material_read_counter + 1;

            MATERIALS{Material_read_counter}.card = 'MAT2';
            MATERIALS{Material_read_counter}.ID = [];
            MATERIALS{Material_read_counter}.Patran_name = [];
            MATERIALS{Material_read_counter}.G11 = 0;
            MATERIALS{Material_read_counter}.G12 = 0;
            MATERIALS{Material_read_counter}.G13 = 0;
            MATERIALS{Material_read_counter}.G22 = 0;
            MATERIALS{Material_read_counter}.G23 = 0;
            MATERIALS{Material_read_counter}.G33 = 0;
            MATERIALS{Material_read_counter}.RHO = [];
            MATERIALS{Material_read_counter}.A1 = [];
            MATERIALS{Material_read_counter}.A2 = [];
            MATERIALS{Material_read_counter}.A3 = [];
            MATERIALS{Material_read_counter}.TREF = [];
            MATERIALS{Material_read_counter}.GE = [];
            MATERIALS{Material_read_counter}.ST = [];
            MATERIALS{Material_read_counter}.SC = [];
            MATERIALS{Material_read_counter}.SS = [];
            MATERIALS{Material_read_counter}.MCSID = [];
            MATERIALS{Material_read_counter}.GE11 = [];
            MATERIALS{Material_read_counter}.GE12 = [];
            MATERIALS{Material_read_counter}.GE13 = [];
            MATERIALS{Material_read_counter}.GE22 = [];
            MATERIALS{Material_read_counter}.GE23 = [];
            MATERIALS{Material_read_counter}.GE33 = [];

            % Чтение ID
            if (length(string) < 24)
                End_index = length(string);
            else
                End_index = 24;
            end
            tmp_number = str2double(string(9:End_index));
            if (isnan(tmp_number))
                fprintf (1, '\nНеправильная запись с номером материала в карточке MAT2* : [%s] !\n', strtrim(string(9:End_index)));
                ErrCode = -1;
                return;
            end
            MATERIALS{Material_read_counter}.ID = tmp_number;

            % проверка на записи в 10-ом поле
            if (deblank(string) > 72)
                fprintf (1, '\nОбнаружена запись в поле 10 первой строки карточки MAT2* %i !\n', MATERIALS{Material_read_counter}.ID);
                fprintf (1, 'Указатели продления карточки не поддерживаются в текущей версии программы.\n');
                fprintf (1, 'Обратетись к разработчику для решения этого вопроса.\n');
                ErrCode = -1;
                return;
            end

            % Чтение Patran_name
            if ((length(Previous_strings{1} ) >= 25) && strcmp(Previous_strings{1} (1:25), '$ Description of Material'))
                if ((length(Previous_strings{2} ) >= 20) && strcmp(Previous_strings{2} (1:19), '$ Material Record :')) % здесь нет ошибки с 20 и 19
                    MATERIALS{Material_read_counter}.Patran_name = strtrim(Previous_strings{2}(20:length(Previous_strings{2})));
                else
                    if ((length(Previous_strings{2} ) >= 3) && strcmp(Previous_strings{2}(1:2), '$ ')) % здесь нет ошибки с 3 и 2
                        if ((length(Previous_strings{3} ) >= 20) && strcmp(Previous_strings{3} (1:19), '$ Material Record :')) % здесь нет ошибки с 20 и 19
                            MATERIALS{Material_read_counter}.Patran_name = [strtrim(Previous_strings{3}(20:length(Previous_strings{3}))) strtrim(Previous_strings{2}(3:length(Previous_strings{2})))];
                        end
                    end
                end
            end

            % --- УНИВЕРСАЛЬНЫЙ АЛГОРИТМ ДЛЯ КАРТОЧЕК С ФИКСИРОВАННЫМ ЧИСЛОМ НЕОБЯЗАТЕЛЬНЫХ ПОЛЕЙ ---

            % Установка информационных параметров
            Card_name = 'MAT2*';
            Item_ID = MATERIALS{Material_read_counter}.ID;

            % Установка стартовых параметров
            Previous_index = 24;
            Record_length = 16;
            Next_string_large = 1;
            Record_read_counter = 1; % Отсчет прочтенных полей начинается со второго поля карточки
            End_card_flag = 0;

            % Чтение полей карточки
            Results_array = cell(15,1);

            for i=1:size(Results_array,1)

                [Read_Result, Fin, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings] = Read_Field_RB(Fin, Card_name, Item_ID, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings);

                if (ErrCode < 0)
                    break;
                end

                Results_array{i} = Read_Result;

            end

            if (ErrCode < 0)
                return;
            end

            % --- G11 ---
            if (~isempty(Results_array{1}))
                MATERIALS{Material_read_counter}.G11 = Results_array{1};
            end

            % --- G12 ---
            if (~isempty(Results_array{2}))
                MATERIALS{Material_read_counter}.G12 = Results_array{2};
            end

            % --- G13 ---
            if (~isempty(Results_array{3}))
                MATERIALS{Material_read_counter}.G13 = Results_array{3};
            end

            % --- G22 ---
            if (~isempty(Results_array{4}))
                MATERIALS{Material_read_counter}.G22 = Results_array{4};
            end

            % --- G23 ---
            if (~isempty(Results_array{5}))
                MATERIALS{Material_read_counter}.G23 = Results_array{5};
            end

            % --- G33 ---
            if (~isempty(Results_array{6}))
                MATERIALS{Material_read_counter}.G33 = Results_array{6};
            end

            % --- RHO ---
            if (~isempty(Results_array{7}))
                MATERIALS{Material_read_counter}.RHO = Results_array{7};
            end

            % --- A1 ---
            if (~isempty(Results_array{8}))
                MATERIALS{Material_read_counter}.A1 = Results_array{8};
            end

            % --- A2 ---
            if (~isempty(Results_array{9}))
                MATERIALS{Material_read_counter}.A2 = Results_array{9};
            end

            % --- A3 ---
            if (~isempty(Results_array{10}))
                MATERIALS{Material_read_counter}.A3 = Results_array{10};
            end

            % --- TREF ---
            if (~isempty(Results_array{11}))
                MATERIALS{Material_read_counter}.TREF = Results_array{11};
            end

            % --- GE ---
            if (~isempty(Results_array{12}))
                MATERIALS{Material_read_counter}.GE = Results_array{12};
            end

            % --- ST ---
            if (~isempty(Results_array{13}))
                MATERIALS{Material_read_counter}.ST = Results_array{13};
            end

            % --- SC ---
            if (~isempty(Results_array{14}))
                MATERIALS{Material_read_counter}.SC = Results_array{14};
            end

            % --- SS ---
            if (~isempty(Results_array{15}))
                MATERIALS{Material_read_counter}.SS = Results_array{15};
            end

            % --- Чтение MCSID --- (integer! используется функция Read_Field_IB)

            [Read_Result, Fin, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings] = Read_Field_IB(Fin, Card_name, Item_ID, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings);

            if (ErrCode < 0)
                return;
            end

            if (~isempty(Read_Result))
                MATERIALS{Material_read_counter}.MCSID = Read_Result;
            end
            
            % Чтение полей GEij
            Results_array = cell(6,1);

            for i=1:size(Results_array,1)

                [Read_Result, Fin, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings] = Read_Field_RB(Fin, Card_name, Item_ID, Previous_index, Record_length, Next_string_large, Record_read_counter, End_card_flag, string, Previous_strings);

                if (ErrCode < 0)
                    break;
                end

                Results_array{i} = Read_Result;

            end

            if (ErrCode < 0)
                return;
            end

            % --- GE11 ---
            if (~isempty(Results_array{1}))
                MATERIALS{Material_read_counter}.GE11 = Results_array{1};
            end

            % --- GE12 ---
            if (~isempty(Results_array{2}))
                MATERIALS{Material_read_counter}.GE12 = Results_array{2};
            end

            % --- GE13 ---
            if (~isempty(Results_array{3}))
                MATERIALS{Material_read_counter}.GE13 = Results_array{3};
            end

            % --- GE22 ---
            if (~isempty(Results_array{4}))
                MATERIALS{Material_read_counter}.GE22 = Results_array{4};
            end

            % --- GE23 ---
            if (~isempty(Results_array{5}))
                MATERIALS{Material_read_counter}.GE23 = Results_array{5};
            end

            % --- GE33 ---
            if (~isempty(Results_array{6}))
                MATERIALS{Material_read_counter}.GE33 = Results_array{6};
            end


            % Для вывода прогресса
            Counter = Counter + 1;
            Counter_2 = Counter_2 + 1;
            if (Counter_2 == N_rec_segm)
                Counter_2 = 0;
                Written_count = Written_count + 1;
                if (Written_count < Time_segments)
                    fprintf (1, '#');
                    pause (Progress_Bar_Delay);
                end
            end


            continue;

        end

        if ((length(string) >= 5) && strcmp(string(2:5), 'AT2,')) % FREE FIELD FORMAT

            fprintf (1, '\nОшибка функции Read_MATERIALS: FREE FIELD FORMAT файла bdf не поддерживается в текущей версии программы [MAT2,] !\n');
            ErrCode = -1;
            return; 

        end
    
    end
    
end


% Для вывода прогресса
if (N_rec_segm == 0)
    for i=1:Time_segments-1
        fprintf (1, '#');
        pause (Progress_Bar_Delay);
    end
end
fprintf (1, '# ');
pause (Progress_Bar_Delay);


% Обработка исключений
if (Material_read_counter < N_Materials)
    fprintf (1, '\nОшибка функции Read_MATERIALS: Material_read_counter < N_Materials !\n');
    ErrCode = -1;
    return;        
end

fprintf (1, 'OK!\n');
